package com.woniuxy.java106crm.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.java106crm.request.Message;
import com.woniuxy.java106crm.request.ResponseResult;
import com.woniuxy.java106crm.request.StateCode;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义的无权限处理器
 */
public class CustomAccessDeniedHandler implements AccessDeniedHandler {


    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        ResponseResult<String> responseResult = new ResponseResult(StateCode.NOPERMS, Message.ACCESS_DENIED,null);
        //利用jackson将对象转换成json字符串
        String json = new ObjectMapper().writeValueAsString(responseResult);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(json);
    }



}
